package com.youlv.yl.util;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.models.*;
import com.youlv.yl.config.SystemConfig;

/**
 * @author fenghe
 * @date 2022/1/2 9:28
 */
public class AliSmsUtils {
    private static Client client;

    // 完成初始化
    static {
        Config config = new Config()
                // 您的AccessKey  ID
                .setAccessKeyId(SystemConfig.ALI_SSM_KEY)
                // 您的AccessKey Secret
                .setAccessKeySecret(SystemConfig.ALI_SSM_SEC);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        try {
            client = new Client(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean sendCode(String temcode, String phone, int code) {
        //1.准备请求参数
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("来自邢朋辉的短信")
                .setTemplateCode(temcode)
                .setTemplateParam("{\"code\":" + code + "}");
        try {
            // 2.发送短信，获取响应结果
            SendSmsResponse response = client.sendSms(sendSmsRequest);
            // 3.返回结果
            return response.getBody().code.equals("OK");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
